package com.wang.huni.util;

import androidx.appcompat.app.AppCompatActivity;

import com.wang.huni.BlueToothTools.BlueToothController;
import com.wang.huni.Fragments.*;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.wang.huni.Fragments.healthFragment;
import com.wang.huni.LoginActivity;
import com.wang.huni.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    RadioGroup radioGroup;
    RadioButton btn_health,btn_huni,btn_me;
    Fragment[] fragments = new Fragment[3];
    Fragment mCurrentFragment;
    private BlueToothController mController = new BlueToothController();
    private Toast mToast;
    private static final int RESULT_CODE = 0;
    private String dvName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        intiEvents();

        radioGroup = findViewById(R.id.group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String text = "";
                switch (checkedId){
                    case R.id.btn_health:
                        text = "健康";
                        break;
                    case R.id.btn_huni:
                        text = "HUNI";
                        break;
                    case R.id.btn_me:
                        text = "我的";
                        break;
                }
                Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 支持蓝牙？
     * @param view
     */
    public void isSupportBlueTooth(View view){
        boolean ret = mController.isSupportBlueTooth();
        showToast("支持蓝牙？" + ret);
    }

    /**
     * 蓝牙打开？
     * @param view
     */
    public void isBlueToothEnable(View view){
        boolean ret = mController.getBlueToothStatus();
        showToast("蓝牙打开？" + ret);
    }

    /**
     * 请求打开蓝牙
     * @param view
     */
    public void requestTurnOnBlueTooth(View view){
        mController.turnOnBlueTooth(this, RESULT_CODE);
    }

    /**
     * 请求关闭蓝牙
     * @param view
     */
    public void requestTurnOffBlueTooth(View view){
        mController.turnOffBlueTooth();
    }

    public void getBluetoothName(View view){
        dvName =  mController.getBlueToothName();
        showToast("已连接设备：" + dvName);
    }

    public void showToast(String text){
        if (mToast == null){
            mToast = Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT);
        }else {
            mToast.setText(text);
        }
        mToast.show();
    }

    /**
     * 加载事件
     */
    private void intiEvents() {
        btn_health.setOnClickListener(this);
        btn_huni.setOnClickListener(this);
        btn_me.setOnClickListener(this);
    }

    /**
     * 加载控件
     */
    private void initView() {
        btn_health = findViewById(R.id.btn_health);
        btn_huni = findViewById(R.id.btn_huni);
        btn_me = findViewById(R.id.btn_me);
    }

    @Override
    public void onClick(View v) {
        showFragment(v.getId());
    }

    private void showFragment(int id) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment fragment = null;
        switch (id) {
            case R.id.btn_health:
                if (fragments[0]!=null){
                    fragment = fragments[0];
                }else {
                    fragment = new healthFragment();
                    fragments[0] = fragment;
                }
                break;
            case R.id.btn_huni:
                if (fragments[1]!=null){
                    fragment = fragments[1];
                }else {
                    fragment = new huniFragment();
                    fragments[1] = fragment;
                }
                break;
            case R.id.btn_me:
                if (fragments[2]!=null){
                    fragment = fragments[2];
                }else {
                    fragment = new meFragment();
                    fragments[2] = fragment;
                }
                break;
        }
//        transaction.replace(R.id.main_page,fragment);
        if (mCurrentFragment!=null){
            transaction.hide(mCurrentFragment);
        }
        if (!fragment.isAdded()){//没有添加过，则添加
            transaction.add(R.id.content,fragment);
        }else {//添加过，则显示
            transaction.show(fragment);
        }

        transaction.commit();
        mCurrentFragment = fragment;
    }

    public void turnToLoginPage(View view) {
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
    }
}